package gl.training.basics;
import static java.lang.Math.*;

class NeighbouringDemo {

	private static int DIFFERENCE_BETWEEN_NEIGHBOURS = 1;

	public static void main(String[] args) {
		//----------------------- inputing -------------------------------
		System.out.println("***** Arrays - neighbouring numbers *****");		
		System.out.println("Input count of numbers then input each numbers " +
						   "of array (One by one, through <Enter> ).");								   
		int countOfNumbers = requestNumber("count = ");
		if(countOfNumbers < 1) return;
		//-- saving numbers&indexes to arrays	
		int[] Numbers = new int[countOfNumbers];
		int[] Indexes = new int[countOfNumbers];
		for(int index=0; index < countOfNumbers; index++) {		
			Numbers[index] = requestNumber("Numbers["+index+"] = ");
			Indexes[index] = index;
		}
		//------------------------ sorting -------------------------------
		boolean some_numbers_exchanged;
		do {
			some_numbers_exchanged = false;
			for(int index=1; index < countOfNumbers; index++) {
				if(Numbers[index-1] > Numbers[index]) {
					// exchanging numbers
					int higher_number = Numbers[index-1];
					Numbers[index-1] = Numbers[index];
					Numbers[index] = higher_number;
					// then, exchanging indexes of numbers
					int position_of_higher_number = Indexes[index-1];
					Indexes[index-1] = Indexes[index];
					Indexes[index] = position_of_higher_number;
					// 
					some_numbers_exchanged = true;
				}				
			}
		}while(some_numbers_exchanged);
		//---------------------- calculating -----------------------------
		int distance_between_nighbours = Integer.MAX_VALUE;
		int index_of_neighbours_couple = -1;
		
		for(int index=1; index < countOfNumbers; index++) {
			int first_number = Numbers[index-1];
			int second_number = Numbers[index];
			int first_position = Indexes[index-1];
			int second_position = Indexes[index];
			if(first_number + DIFFERENCE_BETWEEN_NEIGHBOURS == second_number) {
				int distance = abs(first_position - second_position);
				if(distance <  distance_between_nighbours) {
					distance_between_nighbours = distance;
					index_of_neighbours_couple = min(first_position, second_position);
				}
				if(distance == 1) break;				
			}
		}		
		//------------------------- outputing ----------------------------
		if(distance_between_nighbours == Integer.MAX_VALUE) {
			System.out.println("Answer: Neighbours hasn't found");
		} else {
			System.out.println("Answer: " + index_of_neighbours_couple);
		}		
	}

	private static int requestNumber(String msg) {
		int MAX_DIGITS = 16;
		boolean repeat_input_request;
		int inputed_number = 0;
		do {
			repeat_input_request = false;
			System.out.print(msg);
			try {
				String string_number;
				byte[] buf = new byte[MAX_DIGITS];
				int count_bytes = System.in.read(buf, 0, MAX_DIGITS-1);				
				string_number = new String(buf, 0, count_bytes-2);
				inputed_number = Integer.parseInt(string_number);
			} catch(Exception e) {
				System.out.println("Number is incorrect, please try again ");
				repeat_input_request = true;
			}		
		}while(repeat_input_request);
		return inputed_number;
	}
}